<?php

namespace common\model;

use think\model\RelationModel;

class TenantAuthenticationModel extends RelationModel
{
    const STATUS_SUBMITTING = 'submitting';  // 状态-提交
    const STATUS_TO_BE_REVIEWED = 'to_be_reviewed'; // 状态-待审核
    const STATUS_ACTIVE = 'active';  // 状态-已审核
    const STATUS_INVALID = 'invalid'; // 状态-审核不通过

    const FDD_AUTH_STATUS_NOT_AUTH = 0; // 法大大未认证
    const FDD_AUTH_STATUS_PASS = 4; // 法大大审核通过

    //自动验证
    protected $_validate = [
        ['tenant_id', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['tenant_id', '', '', self::MUST_VALIDATE, 'unique', self::MODEL_INSERT],//租户提交认证唯一性
        ['company_name', '', '', self::MUST_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['company_name', '1,255', '', self::EXISTS_VALIDATE, 'length'],
        ['company_name', '', '', self::EXISTS_VALIDATE, 'unique'],
        ['unified_social_credit_code', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['telephone_number', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['unified_social_credit_code', '1,20', '', self::EXISTS_VALIDATE, 'length'],
        ['unified_social_credit_code', '', '', self::EXISTS_VALIDATE, 'alphaNum', self::MODEL_BOTH],//只能字母和数字
        ['unified_social_credit_code', '', '', self::EXISTS_VALIDATE, 'unique'],
        ['company_business_scope', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['bank_of_deposit', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['bank_of_deposit', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['bank_subbranch', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['bank_subbranch', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['bank_account', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['bank_account', '1,24', '', self::EXISTS_VALIDATE, 'length'],
        ['bank_account_owner', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['bank_account_owner', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['legal_person_name', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['legal_person_name', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['telephone_number', '1,25', '', self::EXISTS_VALIDATE, 'length'],
        ['legal_id_number', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['legal_id_number', '1,18', '', self::EXISTS_VALIDATE, 'length'],
//        ['legal_id_number', '', '', self::EXISTS_VALIDATE, 'unique'],
        ['legal_phone', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['legal_phone', '1,20', '', self::EXISTS_VALIDATE, 'length'],
//        ['legal_phone', '', '', self::EXISTS_VALIDATE, 'unique'],
        ['company_web_site', '1,255', '', self::EXISTS_VALIDATE, 'length'],
        ['company_office_address', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['company_office_address', '1,255', '', self::EXISTS_VALIDATE, 'length'],
        ['person_in_charge', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['person_in_charge', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['person_in_charge_phone', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['person_in_charge_phone', '1,20', '', self::EXISTS_VALIDATE, 'length'],
        ['person_in_charge_email', '1,128', '', self::EXISTS_VALIDATE, 'length'],
        ['company_found_date', '', '', self::EXISTS_VALIDATE, 'require', self::MODEL_INSERT],//必须字段
        ['status', ['active', 'invalid', 'to_be_reviewed', 'submitting'], '', self::EXISTS_VALIDATE, 'in'],
        ['json', '', '', self::EXISTS_VALIDATE, 'array'],
        ['service_category', '', '', self::EXISTS_VALIDATE, 'array'],
        ['company_customer_service', '', '', self::EXISTS_VALIDATE, 'array'],
    ];

    // 自动完成
    protected $_auto = [
        ['updated', 'time', self::MODEL_INSERT, 'function'],
        ['created_by', 'fill_created_by', self::MODEL_INSERT, 'function'],
        ['created', 'time', self::MODEL_INSERT, 'function'],
        ['json', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['service_category', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['company_customer_service', 'json_encode', self::EXISTS_VALIDATE, 'function'],
        ['secrecy_level', 'json_encode', self::EXISTS_VALIDATE, 'function'],
    ];


    // 隐藏字段
    protected $hidden = ['json'];

    /**
     * 获取器：创建时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getCreatedAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器：更新时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getUpdatedAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value, 1);
//        }
//        return "";
//    }

    /**
     * 获取器：经营期限(止), 0为长期
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getOperationPeriodEndAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value);
//        }
//        return "长期";
//    }

    /**
     * 法人身份证有效期限(止), 0为长期
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getLegalIdPeriodEndAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value);
//        }
//        return "长期";
//    }

    /**
     * 公司成立时间
     * @param $value
     * @param $data
     * @return array|mixed
     */
//    public function getCompanyFoundDateAttr($value, $data)
//    {
//        if (!empty($value)) {
//            return get_format_date($value);
//        }
//        return "";
//    }

    /**
     * 获取器：服务类别
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getServiceCategoryAttr($value, $data)
    {
        // 判断是否有值
        if (!empty($value)) {
            $jsonDictData = json_decode($value, true);
        } else {
            $jsonDictData = [];
        }

        return $jsonDictData;
    }

    /**
     * 获取器：曾服务客户
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getCompanyCustomerServiceAttr($value, $data)
    {
        // 判断是否有值
        if (!empty($value)) {
            $jsonDictData = json_decode($value, true);
        } else {
            $jsonDictData = [];
        }

        return $jsonDictData;
    }

    /**
     * 获取器：安全级别
     * @param $value
     * @param $data
     * @return array|mixed
     */
    public function getSecrecyLevelAttr($value, $data)
    {
        // 判断是否有值
        if (!empty($value)) {
            $jsonDictData = json_decode($value, true);
        } else {
            $jsonDictData = [];
        }

        $valueMap = DataSourceModel::getDataSourceByCodeAssoc("secrecy_level");

        foreach ($jsonDictData as &$jsonDictItem) {
            $jsonDictItem = $valueMap[$jsonDictItem] ?? $jsonDictItem;
        }
        return $jsonDictData;
    }

    /**
     * 获取器：硬件设施
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function getOfficeHardwareAttr($value, $data)
    {
        $valueMap = DataSourceModel::getDataSourceByCodeAssoc("office_hardware");
        // 判断是否有值
        if (!empty($value)) {
            $realVal = $valueMap[$value] ?? $valueMap;
        } else {
            $realVal = "";
        }
        return $realVal;
    }

    /**
     * 获取器：公司管理人员
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function getCompanyManagerNumberAttr($value, $data)
    {
        $valueMap = DataSourceModel::getDataSourceByCodeAssoc("company_number");
        // 判断是否有值
        if (!empty($value)) {
            $realVal = $valueMap[$value] ?? $valueMap;
        } else {
            $realVal = "";
        }
        return $realVal;
    }

    /**
     * 获取器：公司制作人员
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function getCompanyMakerNumberAttr($value, $data)
    {
        $valueMap = DataSourceModel::getDataSourceByCodeAssoc("company_number");
        // 判断是否有值
        if (!empty($value)) {
            $realVal = $valueMap[$value] ?? $valueMap;
        } else {
            $realVal = "";
        }
        return $realVal;
    }

    /**
     * 获取器：公司全部人员
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function getCompanyStaffNumberAttr($value, $data)
    {
        $valueMap = DataSourceModel::getDataSourceByCodeAssoc("company_number");
        // 判断是否有值
        if (!empty($value)) {
            $realVal = $valueMap[$value] ?? $valueMap;
        } else {
            $realVal = "";
        }
        return $realVal;
    }

}
